#ifndef unitconverterpanel_h
#define unitconverterpanel_h

#include "UtilityPanel.h"
#include "UtilityButton.h"
#include "../TextField.h"
#include <map>

using std::map;

class UnitConverterPanel: public UtilityPanel {
private:
	ComboBox* boxA;
	ComboBox* boxB;
	TextField* textA;
	//	TextField* textB;
	UtilityButton* calcButton;
	map<int, map<int, float> > conversions;

public:
	UnitConverterPanel() :
		UtilityPanel(T("Unit Converter")) {
		addAndMakeVisible(boxA = new ComboBox(T("Name A")));
		addAndMakeVisible(boxB = new ComboBox(T("Name B")));
		addAndMakeVisible(textA = new TextField(T("Amount")));
		//		addAndMakeVisible(textB = new TextField(T("Amt B")));

		String items[] = { T("g"), T("lb"), T("oz"), T("ml"), T("l"), T("qt") };
		for (int i = 0; i < (int)(sizeof(items) / sizeof(String)); i++) {
			boxA->addItem(items[i], i);
			boxB->addItem(items[i], i);
		}

		addAndMakeVisible(calcButton = new UtilityButton(0, this, T("Calculate")));

		float c[6][6] = { { 1, 453.592f, 28.3495f, -1, -1, -1 }, { 0.0022046f, 1, 16, -1, -1, -1 }, {
				0.035274f, 0.0625f, 1, -1, -1, -1 }, { -1, -1, -1, 1, 1000.f, 946.f }, { -1, -1, -1,
				0.001f, 1, 1 / 0.946f }, { -1, -1, -1, 0.0010571f, 0.946f, 1 } };
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 6; j++) {
				conversions[i][j] = c[i][j];
			}
		}
	}

	~UnitConverterPanel() {
		delete boxA;
		delete boxB;
	}

	void paintPanel(Graphics& g) {
		Font plain(Font::getDefaultSansSerifFontName(), 16, Font::plain);
		g.setFont(plain);

		int idA = boxA->getSelectedId();
		int idB = boxB->getSelectedId();

		float valA = textA->getValue();

		if (conversions[idA][idB] > 0) {
			String s = String::formatted(T("%3.4f"), valA * conversions[idA][idB]);
			g.drawSingleLineText(s, (getWidth() - plain.getStringWidth(s)) / 2, 200);
		} else
			g.drawSingleLineText(String(T("--")), getWidth() / 2 - 7, 200);
	}

	void buttonClicked(int buttonID) {
		repaint();
	}

	void resized() {
		boxA->setBounds(getWidth() / 2 + 15, 70, getWidth() / 2 - 45, 30);
		boxB->setBounds(getWidth() / 2 + 15, 105, getWidth() / 2 - 45, 30);
		textA->setBounds(20, 70, getWidth() / 2 - 20, 30);
		//		textB->setBounds(20, 105, getWidth() / 2 - 20, 30);
		calcButton->setBounds(40, 145, getWidth() - 80, 30);
	}
};

#endif
